﻿

(function () {
    appModule.controller('common.views.basicPersons.createOrEditModal', [
        '$scope', '$uibModalInstance', 'abp.services.app.basicPerson', 'basicPersonId', 'abp.services.app.area', 'actionName', 'basicPerson', 'abp.services.app.specialPerson', 'abp.services.app.user',
        function ($scope, $uibModalInstance, basicPersonService, basicPersonId, areaService, actionName, basicPerson, specialPersonService, userService) {
            var vm = this;
            vm.saving = false;
            //首先将basicPerson数据设置为null
            vm.basicPerson = null;



            //触发保存方法
            vm.save = function () {
                vm.saving = true;
                basicPersonService.createOrUpdateBasicPersonAsync({ basicPersonEditDto: vm.basicPerson }).then(function () {
                    abp.notify.info(app.localize('SavedSuccessfully'));
                    $uibModalInstance.close();
                }).finally(function () {
                    vm.saving = false;
                })

            };
            //取消关闭页面
            vm.cancel = function () {
                $uibModalInstance.dismiss();
            };

            //初始化页面
            function init() {

                //绑定镇控件
                areaService.getAreaByLevelAsync({ id: 0 }).then(function (result) {
                    vm.townList = result.data.items;
                });
                //绑定性别
                var sexData = { data: [{ 'name': '男', 'value': true }, { 'name': '女', 'value': false }] };
                vm.sexList = sexData.data;

                //绑定政治面貌
                specialPersonService.getAllSpePoliticsList().then(function (result) {
                    vm.disPoliticsList = result.data;
                });

                if (actionName == 'edit') {

                    //绑定区控件
                    areaService.getAreaByLevelAsync({ id: basicPerson.townID }).then(function (result) {
                        vm.areaList = result.data.items;
                    });

                    //绑定村控件
                    areaService.getAreaByLevelAsync({ id: basicPerson.areaID }).then(function (result) {
                        vm.villageList = result.data.items;

                        //绑定数据
                        basicPersonService.getBasicPersonForEditAsync({
                            id: basicPersonId
                        }).then(function (result) {
                            vm.basicPerson = result.data.basicPerson;
                        });

                    });

                }
                else if (actionName == 'create') {

                    //获取Session中的userId
                    var userid = abp.session.userId;
                    var tid;
                    var aid;
                    var vid;

                    //获取用户数据
                    userService.getUsersGyUserId({ userid: userid }).then(function (result) {
                        tid = result.data.townID;
                        aid = result.data.areaID;
                        vid = result.data.villageID;

                        //绑定区控件
                        areaService.getAreaByLevelAsync({ id: tid }).then(function (result) {
                            vm.areaList = result.data.items;
                        });

                        //绑定村控件
                        areaService.getAreaByLevelAsync({ id: aid }).then(function (result) {
                            vm.villageList = result.data.items;

                            //绑定数据
                            basicPersonService.getBasicPersonForEditAsync({
                                id: null
                            }).then(function (result) {
                                vm.basicPerson = result.data.basicPerson;
                                vm.basicPerson.parentID = 0;

                                vm.basicPerson.townID = tid;
                                vm.basicPerson.areaID = aid;
                                vm.basicPerson.villageID = vid;
                            });
                        });
                    });
                }
                else if (actionName == 'createChild') {
                    var tid = basicPerson.townID;
                    var aid = basicPerson.areaID;
                    var vid = basicPerson.villageID;

                    //绑定区控件
                    areaService.getAreaByLevelAsync({ id: tid }).then(function (result) {
                        vm.areaList = result.data.items;
                    });

                    //绑定村控件
                    areaService.getAreaByLevelAsync({ id: aid }).then(function (result) {
                        vm.villageList = result.data.items;

                        //绑定数据
                        basicPersonService.getBasicPersonForEditAsync({
                            id: null
                        }).then(function (result) {
                            vm.basicPerson = result.data.basicPerson;
                            vm.basicPerson.parentID = basicPersonId;
                            vm.basicPerson.totalPopulation = basicPerson.totalPopulation + 1;
                            vm.basicPerson.houseNumber = basicPerson.houseNumber;

                            vm.basicPerson.townID = tid;
                            vm.basicPerson.areaID = aid;
                            vm.basicPerson.villageID = vid;
                        });
                    });
                }
            }

            //绑定区
            vm.bindArea = function (townID) {
                areaService.getAreaByLevelAsync({ id: townID }).then(function (result) {
                    vm.areaList = result.data.items;
                });
            };

            //绑定村
            vm.bindVillage = function (areaID) {
                areaService.getAreaByLevelAsync({ id: areaID }).then(function (result) {
                    vm.villageList = result.data.items;
                });
            };

            //执行初始化方法
            init();
        }
    ]);
})();